using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using WebApp8.Client.Services;

namespace WebApp8.Client.ViewModels
{
    public partial class MainViewModel : ObservableObject
    {
        private readonly ApiService _apiService;

        [ObservableProperty]
        private ObservableCollection<WeatherForecast> _weatherForecasts = new();

        [ObservableProperty]
        private string _status = string.Empty;

        [ObservableProperty]
        private bool _isLoading;

        public MainViewModel()
        {
            _apiService = new ApiService();
        }

        [RelayCommand]
        private async Task LoadWeatherForecast()
        {
            try
            {
                IsLoading = true;
                Status = "Loading...";
                WeatherForecasts.Clear();

                var forecasts = await _apiService.GetWeatherForecastAsync();
                if (forecasts != null)
                {
                    foreach (var forecast in forecasts)
                    {
                        WeatherForecasts.Add(forecast);
                    }
                    Status = $"Loaded {forecasts.Count} forecasts";
                }
            }
            catch (Exception ex)
            {
                Status = $"Error: {ex.Message}";
            }
            finally
            {
                IsLoading = false;
            }
        }
    }
} 